﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace ProjectRoughWork
{
    class Crate : Cover
    {
        /// <summary>
        /// 2-Parameter Default Constructor
        /// </summary>
        /// <param name="initPos">The initial position of the crate</param>
        /// <param name="initSize">The initial size of the crate</param>

        public Crate(Vector3 initPos, float initSize) : base()
        {
            width = initSize;
            height = initSize;
            depth = initSize;
            size = initSize;

            texture = Game1.GetContentManager().Load<Texture2D>("crate");
            mPos = initPos;
            Init();

            mOrigin = new Vector3(mPos.X + size / 2, mPos.Y + size / 2, mPos.Z + size / 2);
        }

        /// <summary>
        /// 4-Parameter Constructor to detail the width, height and depth of the crate
        /// </summary>
        /// <param name="initPos">The position of the crate</param>
        /// <param name="initWidth">The width of the crate (X-Axis)</param>
        /// <param name="initHeight">The height of the crate (Y-Axis)</param>
        /// <param name="initDepth">The depth of the crate (Z-Axis)</param>

        public Crate(Vector3 initPos, float initWidth, float initHeight, float initDepth)
        {
            width = initWidth;
            height = initHeight;
            depth = initDepth;

            texture = Game1.GetContentManager().Load<Texture2D>("crate");
            mPos = initPos;
            Init();

            mOrigin = new Vector3(mPos.X + width / 2, mPos.Y + height / 2, mPos.Z + depth / 2);
        }

        /// <summary>
        /// Initializes the vertices and indices ncessary to draw the crate
        /// </summary>

        public void Init()
        {


            Vector3 normal = new Vector3(0, 0, 1);
            vertices = new VertexPositionNormalTexture[8];

            vertices[0].Position = new Vector3(mPos.X, mPos.Y, mPos.Z);
            vertices[0].Normal = normal;
            vertices[0].TextureCoordinate = new Vector2(0, 1);

            vertices[1].Position = new Vector3(mPos.X, mPos.Y + height, mPos.Z);
            vertices[1].Normal = normal;
            vertices[1].TextureCoordinate = new Vector2(0, 0);

            vertices[2].Position = new Vector3(mPos.X + width, mPos.Y + height, mPos.Z);
            vertices[2].Normal = normal;
            vertices[2].TextureCoordinate = new Vector2(1, 0);

            vertices[3].Position = new Vector3(mPos.X + width, mPos.Y, mPos.Z);
            vertices[3].Normal = normal;
            vertices[3].TextureCoordinate = new Vector2(1, 1);

            vertices[4].Position = new Vector3(mPos.X, mPos.Y, mPos.Z + depth);
            vertices[4].Normal = normal;
            vertices[4].TextureCoordinate = new Vector2(1, 1);

            vertices[5].Position = new Vector3(mPos.X, mPos.Y + height, mPos.Z + depth);
            vertices[5].Normal = normal;
            vertices[5].TextureCoordinate = new Vector2(0, 1);

            vertices[6].Position = new Vector3(mPos.X + width, mPos.Y + height, mPos.Z + depth);
            vertices[6].Normal = normal;
            vertices[6].TextureCoordinate = new Vector2(0, 0);

            vertices[7].Position = new Vector3(mPos.X + width, mPos.Y, mPos.Z + depth);
            vertices[7].Normal = normal;
            vertices[7].TextureCoordinate = new Vector2(0, 1);

            short[] temp = {
                               0,1,2,       //Front
                               2,3,0,
                               4,5,1,       //Left
                               1,0,4,
                               4,5,6,       //Back
                               6,7,4,
                               3,2,6,       //Right
                               6,7,3,
                               0,4,7,       //Bottom
                               7,3,0,
                               1,5,6,       //Top
                               6,2,1
                           };

            indices = temp;
        }

        public float Width
        {
            get { return width; }
        }

        public float Height
        {
            get { return height; }
        }

        public float Depth
        {
            get { return depth; }
        }

        /// <summary>
        /// Draws the crate
        /// </summary>

        public override void VRender()
        {
            effect = Game1.GetBasicEffect();

            effect.EnableDefaultLighting();
            effect.LightingEnabled = true;

            effect.World = Matrix.Identity;

            if (width != depth)
            {
                //effect.World = Matrix.CreateTranslation(-mPos) * Matrix.CreateRotationY(45) * Matrix.CreateTranslation(mPos);
            }


            effect.TextureEnabled = true;
            effect.Texture = texture;
            
            effect.View = view;
            effect.Projection = projection;

            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();

                Game1.GetGraphicsDevice().DrawUserIndexedPrimitives<VertexPositionNormalTexture>(PrimitiveType.TriangleList, vertices, 0, 8, indices, 0, 12);
            }

            effect.World = Matrix.Identity;
        }

        //Data Members

        Texture2D texture;
        VertexPositionNormalTexture[] vertices;
        short[] indices;

        //Vector3 mPos;
        float size;
        float width;
        float height;
        float depth;

        BasicEffect effect;
    }
}
